using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using Geekeradmin.Entitys;

namespace Geekeradmin.Filters
{
    /// <summary>
    /// 实现ActionFilterAttribute抽象父类，在执行的时候，只会执行异步版本的方法，在源码中他是直接判断了，如果有异步版本，同步版本就不执行了
    /// </summary>
    public class CustomActionResult:ActionFilterAttribute
    {
        private readonly ILogger<CustomActionResult> logger;
        public CustomActionResult(ILogger<CustomActionResult> logger)
        {
            this.logger = logger;
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var paramsdata = String.Join('&', context.HttpContext.Request.Query.Select(o => { return string.Format("{0}={1}", o.Key, o.Value); }));
            //var paramsdatab = context.HttpContext.Request.QueryString.Value;
            var controller = context.HttpContext.GetRouteValue("controller");
            var action = context.HttpContext.GetRouteValue("action");
            var info = $"控制器{controller}执行方法{action}的参数是{paramsdata}";
            //Console.WriteLine(info);
            //this.logger.LogInformation(info);
        }

        public override void OnActionExecuted(ActionExecutedContext context)
        {
            var paramsdata = String.Join('&', context.HttpContext.Request.Query.Select(o => { return string.Format("{0}={1}", o.Key, o.Value); }));
            //var paramsdata = context.HttpContext.Request.QueryString.Value;
            var controller = context.HttpContext.GetRouteValue("controller");
            var action = context.HttpContext.GetRouteValue("action");
            var result = JsonConvert.SerializeObject(context.Result);
            var info = $"控制器{controller}执行方法{action}的参数是{paramsdata}的结果是{result}";
            //Console.WriteLine(info);
            //this.logger.LogInformation(info);
        }

        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var paramsdata = String.Join('&', context.HttpContext.Request.Query.Select(o => { return string.Format("{0}={1}", o.Key, o.Value); }));
            //var paramsdata = context.HttpContext.Request.QueryString.Value;
            var controller = context.HttpContext.GetRouteValue("controller");
            var action = context.HttpContext.GetRouteValue("action");
            ActionExecutedContext executedContext = await next.Invoke();//执行Action
            var result = JsonConvert.SerializeObject(executedContext.Result);
            var info = $"控制器{controller}执行方法{action}的参数是{paramsdata}的结果是{result}";
           // Console.WriteLine(info);
            //this.logger.LogInformation(info);
        }

        public override void OnResultExecuting(ResultExecutingContext context)
        {
            if (context.Result is JsonResult)
            {
                JsonResult result = (JsonResult)context.Result;
                context.Result = new JsonResult(new AjaxResult()
                {
                    Success = true,
                    Message = "OK",
                    Data = result.Value,
                    dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                });
            }
        }

        public override void OnResultExecuted(ResultExecutedContext context)
        {
            base.OnResultExecuted(context);
        }

        public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            if (context.Result is JsonResult)
            {
                JsonResult result = (JsonResult)context.Result;
                context.Result = new JsonResult(new AjaxResult()
                {
                    Success = true,
                    Message = "OK",
                    Data = result.Value,
                    dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                });
            }
            await next.Invoke();
        }


    }
}
